package com.celan.year2023.month01.day29;

import java.util.*;

public class Solution {
    public int countAsterisks(String s) {
        int n = s.length();
        int cnt = 0;
        int idx = 0;
        while (idx < n) {
            //统计*号
            while (idx < n && s.charAt(idx) != '|') {
                if (s.charAt(idx) == '*') cnt++;
                idx++;
            }
            //找到第一个|
            while (idx < n && s.charAt(idx) != '|') idx++;
            if (idx < n) idx++;
            //跳过
            while (idx < n && s.charAt(idx) != '|') idx++;
            //统计*号
            if (idx < n) idx++;
            while (idx < n && s.charAt(idx) != '|') {
                if (s.charAt(idx) == '*') cnt++;
                idx++;
            }
        }
        return cnt;
    }
    //"*||*|||||*|*|***||*||***|"

    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean flagCol0 = false;
        for (int i = 0; i < m; i++) {
            if (matrix[i][0] == 0) {
                flagCol0 = true;
            }
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }
        for (int i = m - 1; i >= 0; i--) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
            if (flagCol0) {
                matrix[i][0] = 0;
            }
        }
    }

    public int numDecodings(String s) {
        int n = s.length();
        s = " " + s;
        int[] dp = new int[n + 1];
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
            int a = s.charAt(i) - '0';
            int b = (s.charAt(i - 1) - '0') * 10 + a;
            if (a >= 1 && a <= 9) dp[i] = dp[i - 1];
            if (b >= 10 && b <= 26) dp[i] += dp[i - 2];
        }
        return dp[n];
    }

    //锯齿形遍历即层序遍历，但是奇数为正序，偶数为倒序
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if (root == null) return new ArrayList<>();

        Queue<TreeNode> que = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        boolean reverse = false;

        que.add(root);
        while (!que.isEmpty()) {
            int size = que.size();
            List<Integer> row = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = que.poll();
                row.add(node.val);
                if (node.left != null) que.add(node.left);
                if (node.right != null) que.add(node.right);
            }
            if (reverse) {
                Collections.reverse(row);
                reverse = false;
            } else reverse = true;
            res.add(row);
        }
        return res;
    }

    //先进行bfs，将每层结果连接成链表
    public Node connect(Node root) {
        if (root == null) return null;
        Queue<Node> que = new LinkedList<>();

        que.add(root);
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                Node poll = que.poll();
                if (i < size - 1) poll.next = que.peek();
                if (poll.left != null) {
                    que.add(poll.left);
                    que.add(poll.right);
                }
            }
        }
        return root;
    }
}
